package com.supplychainapi.service.phone.cust;

import com.joindata.inf.common.basic.exceptions.ServiceException;
import com.supplychainapi.common.BizCode;
import com.supplychainapi.common.enums.ExceptionMessage;
import com.supplychainapi.dao.prod.ScmTbcustMapper;
import com.supplychainapi.model.ScmTbcust;
import com.supplychainapi.model.ScmTbcustExtend;
import com.supplychainapi.model.ScmTbseqno;
import com.supplychainapi.model.ScmTbtreebuild;
import com.supplychainapi.service.IScmTbtreebuildService;
import com.supplychainapi.service.TbSeqNoService;
import com.supplychainapi.utils.Constants;
import com.supplychainapi.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shenhong on 2017/10/14.
 */
@Service
public class ScmTbcustPhoneServiceImpl implements IScmTbcustPhoneService {

    private Logger logger = LoggerFactory.getLogger(ScmTbcustPhoneServiceImpl.class);

    @Autowired
    private ScmTbcustMapper scmTbcustMapper;

    @Autowired
    TbSeqNoService tbSeqNoService;
    @Autowired
    private IScmTbtreebuildService treebuildService;

    @Override
    public void addScmTbcustPhoneInfo(ScmTbcust cust) throws ServiceException {
        logger.info("手机版新增供应商");
        if (StringUtil.checkIsEmoji(cust.getFsCustName())) {
            throw ExceptionMessage.emoji_failed.getServiceException();
        }
        if (cust.getFsCustId() == null || cust.getFsCustId().trim().equals("")) {
            logger.info("----开始自动生成编号-----");
            ScmTbseqno model = new ScmTbseqno();
            model.setFsShopGUID(cust.getFsShopGUID());
            model.setFsCls(Constants.CUST_CLASS);
            String fsSupplierId = tbSeqNoService.selectSeqNo(model);
            cust.setFsCustId(fsSupplierId);
            tbSeqNoService.checkCode(model);
            tbSeqNoService.insert(model, cust.getFsShopGUID());
        } else {
            logger.info("手动填写编号业务处理");
            ScmTbseqno tbseqno1 = new ScmTbseqno();
            tbseqno1.setFiSeqNo(cust.getFsCustId());
            tbseqno1.setFsCls(Constants.CUST_CLASS);
            tbSeqNoService.checkCode(tbseqno1);
            tbSeqNoService.insert(tbseqno1, cust.getFsShopGUID());
        }
        //supplier.setFsNodeFullText(supplier.getFsNodeFullText().concat("_"+supplier.getFsSupplierName()));

        //根据挂载的节点ID，找到父级的的信息
        String fsNodeCode = cust.getFsNodeCode();
        List<ScmTbtreebuild> treeList = treebuildService.getParentNodeCodeAndNodeCodeInfo(cust.getFsShopGUID(), Constants.CUST_CLASS, fsNodeCode);
        String fsNodeFullText = "";
        if (treeList != null && treeList.size() > 0) {
            for (ScmTbtreebuild scmTbtreebuild : treeList) {
                fsNodeFullText = fsNodeFullText.concat(scmTbtreebuild.getFsNodeText() + "_");
            }
        }
        cust.setFsNodeFullText(fsNodeFullText.concat(cust.getFsCustName()));
        cust.setFsCreateTime(new Date());
        cust.setFsUpdateTime(new Date());
        scmTbcustMapper.insertSelective(cust);
 /*       logger.info("手机版新增客户");
        if (StringUtil.checkIsEmoji(cust.getFsCustName())) {
            throw ExceptionMessage.emoji_failed.getServiceException();
        }
        if(cust.getFsCustId()==null || cust.getFsCustId().trim().equals("")){
            logger.info("----开始自动生成编号-----");
            ScmTbseqno model = new ScmTbseqno();
            model.setFsShopGUID(cust.getFsShopGUID());
            model.setFsCls(Constants.SUPPLIER_CLASS);
            String fsSupplierId = tbSeqNoService.selectSeqNo(model);
            cust.setFsCustId(fsSupplierId);
        }else{
            logger.info("手动填写编号业务处理");
            ScmTbseqno tbseqno1 = new ScmTbseqno();
            tbseqno1.setFiSeqNo(cust.getFsCustId());
            tbseqno1.setFsCls(Constants.SUPPLIER_CLASS);
            tbSeqNoService.insert(tbseqno1, cust.getFsShopGUID());
        }
        cust.setFsNodeFullText(cust.getFsNodeFullText().concat("_" + cust.getFsCustName()));
        cust.setFsCreateTime(new Date());
        cust.setFsUpdateTime(new Date());
        scmTbcustMapper.insertSelective(cust);*/
    }

    @Override
    public void updateScmTbcustPhoneInfo(ScmTbcust cust) throws ServiceException {

        logger.info("更新供应商信息fsNodeCode = " + cust.getFsNodeCode());
        List<ScmTbtreebuild> treeList = treebuildService.getParentNodeCodeAndNodeCodeInfo(cust.getFsShopGUID(), Constants.CUST_CLASS, cust.getFsNodeCode());
        String fsNodeFullText = "";
        if (treeList != null && treeList.size() > 0) {
            for (ScmTbtreebuild scmTbtreebuild : treeList) {
                fsNodeFullText = scmTbtreebuild.getFsNodeFullText();
            }
        }
        cust.setFsNodeFullText(fsNodeFullText.concat("_" + cust.getFsCustName()));
        cust.setFsUpdateTime(new Date());
        int countPrimaryKeySelective = scmTbcustMapper.updateByPrimaryKeySelective(cust);

        logger.info("更新影响行数 countPrimaryKeySelective={}", countPrimaryKeySelective);
    }

    public ScmTbcustExtend getCustPhoneInfo(String fsCustId) {
        ScmTbcustExtend custDetailsInfo = scmTbcustMapper.getCustInfo(fsCustId);
        return custDetailsInfo;
    }
    @Override
    public ScmTbcust getCustByFsShopGUIDPhoneList(String fsShopGUID, String fsCustId)
            throws ServiceException {
        ScmTbcust scmTbcust = scmTbcustMapper.getCustByFsShopGUIDList(fsShopGUID,fsCustId);
        return scmTbcust;
    }

    @Override
    public void deleteCustPhoneInfo(List<String> fsCustIdList, List<String> fsShopGUIDList) throws ServiceException {
        if(fsCustIdList==null || fsCustIdList.size()<=0){
            throw new ServiceException(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getDesc());
        }

        if(fsShopGUIDList==null || fsShopGUIDList.size()<=0){
            throw new ServiceException(BizCode.FAIL.getNum(), BizCode.PARAM_NULL.getDesc());
        }

        Map<String, Object> parameterMap = new HashMap<String, Object>();
        parameterMap.put("fsSupplierIdList", fsCustIdList);
        parameterMap.put("fsShopGUIDList", fsShopGUIDList);
        scmTbcustMapper.deleteCustInfo(parameterMap);
    }

    @Override
    public List<ScmTbcust> getByKeyWordCustInfoPhoneList(String keyWord, Integer page, Integer pageSize)
            throws ServiceException {
        List<ScmTbcust> scmTbcustList = scmTbcustMapper.getByKeyWordCustInfoList(keyWord,page,pageSize);
        return scmTbcustList;
    }

    @Override
    public List<ScmTbcust> getCustPhoneListInfo(String fsShopGUID, String fsNodeCode, Integer page, Integer pageSize)
            throws ServiceException {
        logger.info("fsShopGUID={},fsNodeCode={},page={},pageSize={}",fsShopGUID,fsNodeCode,page,pageSize);
        if(page!=null){
            page = (page-1) * pageSize;
        }else{
            page = 0;
        }
        List<ScmTbcust> custListInfo = scmTbcustMapper.getCustListInfo(fsShopGUID,fsNodeCode,page,pageSize);
        return custListInfo;
    }

    @Override
    public List<ScmTbcust> getCustPhoneByFsCustNameListInfo(String fsShopGUID, String fsCustName,
                                                            Integer page, Integer pageSize) throws ServiceException {
        logger.info("fsShopGUID={},fsCustName={},page={},pageSize={}",fsShopGUID,fsCustName,page,pageSize);
        if(page!=null){
            page = (page-1) * pageSize;
        }else{
            page = 0;
        }
        List<ScmTbcust> custByNameListInfo = scmTbcustMapper.getCustByFsCustNameListInfo(fsShopGUID,fsCustName,page,pageSize);
        return custByNameListInfo;
    }

    @Override
    public List<ScmTbcustExtend> getCustAllPhoneListInfo(String fsShopGUID, Integer page, Integer pageSize) throws ServiceException {
        logger.info("fsShopGUID={},page={},pageSize={}",fsShopGUID,page,pageSize);
        if(page!=null){
            page = (page-1) * pageSize;
        }else{
            page = 0;
        }
        List<ScmTbcustExtend> custAllListInfo = scmTbcustMapper.getCustAllPhoneListInfo(fsShopGUID, page, pageSize);
        return custAllListInfo;
    }

    @Override
    public Boolean getCustNameIsExit(String fsShopGUID, String fsCustName, String fsCustId, String type) throws ServiceException {
        logger.info("判断客户名称是否重复 fsShopGUID={},fsStorageName={},fsStorageId={},type={}", fsShopGUID, fsCustName, fsCustId, type);
        Integer countNumber = scmTbcustMapper.getCustNameIsExit(fsShopGUID, fsCustName);
        if (type.equals("update")) {
            //当前传入的供应用名称和数据库查询出来的名称相同不做处理
            ScmTbcust cust = scmTbcustMapper.getCustByFsShopGUIDList(fsShopGUID, fsCustId);
            if (cust != null && cust.getFsCustName().equals(fsCustName)) {
                return false;
            }
        }
        logger.info("根据当前门店和客户名查询返回结果集=" + countNumber);
        return countNumber != null && countNumber > 0;
    }

    @Override
    public List<ScmTbcust> getCustParentAllPhoneListInfo(String fsShopGUID, String fsNodeCode, Integer page,
                                                         Integer pageSize) throws ServiceException {
        logger.info("fsShopGUID={},page={},pageSize={}",fsShopGUID,page,pageSize);
        if(page!=null){
            page = (page-1) * pageSize;
        }else{
            page = 0;
        }
        List<ScmTbcust> custParentAllListInfo = scmTbcustMapper.getCustParentAllPhoneListInfo(fsShopGUID,fsNodeCode,page,pageSize);
        return custParentAllListInfo;
    }




}

